/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PersonDetailUI.java
 *
 * Created on 2010.11.09., 13:24:31
 */
package workshopdesign.UI;

import javax.swing.border.TitledBorder;

import workshopdesign.Model.Person;
import workshopdesign.Control.AddressController;

/**
 *
 * @author Dom
 */
public class PersonDetailUI extends javax.swing.JPanel {

    private enum State {

        Empty, Editing
    };
    private State state = State.Empty;
    private Person person = null;
    private AddressController address_book = AddressController.getAddressController();
    private BaseUIController parent;

    /** Creates new form PersonDetailUI */
    public PersonDetailUI(BaseUIController parent, String title, boolean editable) {
        initComponents();

        this.parent = parent;

        this.setBorder(new TitledBorder(title));

        this.name_field.setEditable(editable);
        this.address_field.setEditable(editable);
        this.postal_code_field.setEditable(editable);
        this.city_field.setEditable(editable);
        this.phone_field.setEditable(editable);
        this.email_field.setEditable(editable);

        this.ok_button.setVisible(editable);
        this.cancel_button.setVisible(editable);

        clear_form();
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
        this.update_status();
        this.load_data();
    }

    private void create_person() {
        if (name_field.getText().trim().length() == 0) {
            return;
        }
        
        address_book.createPerson(
                name_field.getText(),
                address_field.getText(),
                postal_code_field.getText(),
                city_field.getText(),
                phone_field.getText(),
                email_field.getText());
        clear_form();

        parent.update_data();
    }

    private void clear_form() {
        this.name_field.setText("");
        this.address_field.setText("");
        this.city_field.setText("");
        this.postal_code_field.setText("");
        this.phone_field.setText("");
        this.email_field.setText("");
    }

    public void load_data() {
        if (person == null) {
            clear_form();
            return;
        }

        this.name_field.setText(person.getName());
        this.address_field.setText(person.getAddress());
        this.city_field.setText(person.getCity());
        this.postal_code_field.setText(person.getPostalCode());
        this.phone_field.setText(person.getPhone());
        this.email_field.setText(person.getEmail());
    }

    private void save_person_data() {
        address_book.updatePerson(
                person.getId(),
                name_field.getText(),
                address_field.getText(),
                postal_code_field.getText(),
                city_field.getText(),
                phone_field.getText(),
                email_field.getText());
        load_data();

        parent.update_data();
    }

    public void clear_person() {
        person = null;
        clear_form();
        update_status();
    }

    public void update_status() {
        state = person == null ? State.Empty : State.Editing;
        update_button_texts();
    }

    public void update_button_texts() {
        if (state == State.Empty) {
            ok_button.setText("Create");
            cancel_button.setText("Clear");
            return;
        }
        if (state == State.Editing) {
            ok_button.setText("Save");
            cancel_button.setText("Cancel");
            return;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ok_button = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        name_field = new javax.swing.JTextField();
        address_field = new javax.swing.JTextField();
        city_field = new javax.swing.JTextField();
        postal_code_field = new javax.swing.JTextField();
        phone_field = new javax.swing.JTextField();
        email_field = new javax.swing.JTextField();
        cancel_button = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getResourceMap(PersonDetailUI.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        ok_button.setText(resourceMap.getString("ok_button.text")); // NOI18N
        ok_button.setName("ok_button"); // NOI18N
        ok_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ok_buttonMouseClicked(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        name_field.setName("name_field"); // NOI18N

        address_field.setName("address_field"); // NOI18N

        city_field.setName("city_field"); // NOI18N

        postal_code_field.setName("postal_code_field"); // NOI18N

        phone_field.setName("phone_field"); // NOI18N

        email_field.setName("email_field"); // NOI18N

        cancel_button.setText(resourceMap.getString("cancel_button.text")); // NOI18N
        cancel_button.setName("cancel_button"); // NOI18N
        cancel_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancel_buttonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 248, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(email_field, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(phone_field, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(address_field, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(city_field, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(postal_code_field, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(name_field, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ok_button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 130, Short.MAX_VALUE)
                        .addComponent(cancel_button)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 192, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(address_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(postal_code_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phone_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(email_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok_button)
                    .addComponent(cancel_button)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ok_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ok_buttonMouseClicked
        if (state == State.Empty) {
            create_person();
            return;
        }
        if (state == State.Editing) {
            save_person_data();
            return;
        }
    }//GEN-LAST:event_ok_buttonMouseClicked

    private void cancel_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancel_buttonMouseClicked
        if (state == State.Empty) {
            clear_form();
            return;
        }
        if (state == State.Editing) {
            setPerson(null);
            parent.clear_selection();
            return;
        }
    }//GEN-LAST:event_cancel_buttonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField address_field;
    private javax.swing.JButton cancel_button;
    private javax.swing.JTextField city_field;
    private javax.swing.JTextField email_field;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField name_field;
    private javax.swing.JButton ok_button;
    private javax.swing.JTextField phone_field;
    private javax.swing.JTextField postal_code_field;
    // End of variables declaration//GEN-END:variables
}
